#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 分红配股
爬取类目：分红(不包含配股)
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/dividend/szmb000001.html
"""
import sys
import json
from xtls.logger import get_logger

from company_formaltable_crawler import CompanyFormalTable

reload(sys)
sys.setdefaultencoding('utf-8')

"""
result_sample_of_company_dividend = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'dividend': [{
        'dividendYear': '',
        'dividendPlan': '',
        'recordDate': '',
        'rightOutDate': '',
        'bonusOfferDate': '',
    },],
}
"""

def get_detail_mapping():
    DETAIL_MAPPING = {
        u'分红年度': 'dividendYear',
        u'分红方案': 'dividendPlan',
        u'股权登记日': 'recordDate',
        u'除权基准日'	: 'rightOutDate',
        u'红股上市日': 'bonusOfferDate',
    }
    return DETAIL_MAPPING

class CompanyDividend(CompanyFormalTable):
    def __init__(self, stock_list):
        logger = get_logger(__file__)
        super(CompanyDividend, self).__init__(stock_list=stock_list, logger=logger)
        self.category = 'dividend'
        self.tab_name = 'dividend'
        self.detail_mapping = get_detail_mapping()

    def clean_data(self, dirty_data):
        self.cleaner.set_dirty_data(dirty_data)
        return self.cleaner.complete()

    def save_update(self, data):
        dividend_list = data.get('dividend')
        if not dividend_list:
            self.logger.warn('dividend has no information %s-%s' % (data.get('stock_id'), data.get('stock_code')))
            return

        if not data.get('stock_id'):
            return
        version_num = 1 + self.select_current_version_num(table_name='dividend',
                                                          stock_id=data.get('stock_id'))
        for single in dividend_list:
            single_clean = self.clean_data(single)  # 对single中的每个数据去开头和结尾空格, 如果是空str就返回None
            single_clean['stockId'] = data.get('stock_id')
            single_clean['versionNum'] = version_num

            self.insert_to_db(table_name='dividend', cleaned_data=single_clean)

    def save_print(self, data):
        print json.dumps(data, indent=4)
        print "=" * 70


# def main():
#     pass

# if __name__ == '__main__':
#     main()

